<template>
  <div class="flexcen">
    <el-form
      @submit.native.prevent
      ref="form"
      :model="user"
      :rules="rules"
      label-width="120px"
    >
      <!-- 实现验证码输入 -->
      <el-form-item label="账号(手机号码)">
        {{ propUser.phoneNumber }}
      </el-form-item>
      <el-form-item label="验证码" prop="verificationCode">
        <el-input
          v-input-restrict
          style="width: 400px"
          placeholder="请输入验证码"
          v-model="user.verificationCode"
        >
          <template slot="append">
            <div
              class="shou"
              style="color: #28b28b"
              @click="startCountdown"
              v-if="!countingDown"
            >
              获取验证码
            </div>
            <div v-else>
              <div v-if="countdownSeconds > 0">
                {{ countdownSeconds }}s后重新获取
              </div>
              <div
                v-else
                style="cursor: pointer; color: #28b28b"
                @click="startCountdown"
              >
                重新获取
              </div>
            </div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input
          v-input-restrict
          style="width: 400px"
          v-model="user.newPassword"
          placeholder="请输入新密码,设置密码:6-16位字母和数字"
          type="password"
          show-password
        />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input
          v-input-restrict
          style="width: 400px"
          v-model="user.confirmPassword"
          placeholder="请确认新密码,设置密码:6-16位字母和数字"
          type="password"
          show-password
        />
      </el-form-item>
      <el-form-item>
        <el-button v-preventReClick type="danger" size="mini" @click="close"
          >返回</el-button
        >
        <el-button v-preventReClick type="primary" size="mini" @click="submit"
          >确认</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { updatePwd, sendPhoneCode } from "@/api/system/user";
import { checkPassword } from "@/utils/validate";
export default {
  props: {
    propUser: {
      type: Object,
    },
  },
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      // 验证码
      countingDown: false,
      countdownSeconds: 60,
      //
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined,
        verificationCode: undefined,
      },
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: "旧密码不能为空", trigger: "blur" },
        ],
        newPassword: [
          { required: true, validator: checkPassword, trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
        confirmPassword: [
          { required: true, validator: checkPassword, trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" },
        ],
        verificationCode: [
          { required: true, message: "验证码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    async startCountdown() {
      await sendPhoneCode({
        phone: this.propUser.phoneNumber,
      });
      this.countdownSeconds = 59; // 重置倒计时时间
      this.countingDown = true;
      const timer = setInterval(() => {
        this.countdownSeconds--;
        if (this.countdownSeconds === 0) {
          clearInterval(timer);
          this.countingDown = true;
        }
      }, 1000);
    },
    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updatePwd({
            phoneNumber: this.propUser.phoneNumber,
            phoneCode: this.user.verificationCode,
            newPassword: this.user.newPassword,
          }).then((response) => {
            this.$modal.msgSuccess("修改成功");
            setTimeout(() => {
              this.close();
            }, 500);
          });
        }
      });
    },
    close() {
      this.$emit("changeTab", "userinfo");
    },
  },
};
</script>
